﻿package com.boyaa.shells.tool 
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.ColorTransform;
	import flash.geom.Rectangle;
	import flash.utils.getDefinitionByName;
	/**
	 * ...
	 * @author fly
	 */
	public class Score
	{
		static private var _gap:int = 10;						// 间隔 
		static private var _allMC:Array = [];
		/**
		 *  返回数字 注册点为中心
		 * @param	num      数字
		 * @param	sign     1  - ; 2 + ; 3 度
		 * @param	grounding	背景色
		 * @param	scoreColor	数字颜色
		 * @return
		 */// grounding:uint = 0xffff00, scoreColor:uint = 0xff0000
		static public function show(num:Number, sign:int = 0, grounding:uint = 0x0D374F, scoreColor:uint = 0xFFCC00) :DisplayObject
		{
			var dis:Sprite = new Sprite();
			var str:String = String(num);
			var count:int = 0;
			var score:MovieClip;
			if (sign == 1)
			{
				score = getScore(12 , grounding , scoreColor);
				score.x = 0;
				dis.addChild(score);
				count ++;
			}
			else if(sign == 2)
			{
				score = getScore(13, grounding , scoreColor);
				score.x = 0;
				dis.addChild(score);
				count ++;
			}
			for (var i:int = 0; i < str.length; i++) 
			{
				
				var frame:int = 0;
				if (str.charAt(i) == ".")	
					frame = 13;
				else
					frame = int(str.charAt(i));
				
				score = getScore(frame + 1, grounding , scoreColor);
				dis.addChild(score);
				score.x = count * _gap;
				count ++;
			}
			if(sign == 3)
			{
				score = getScore(11, grounding , scoreColor);
				dis.addChild(score);
				score.x = count * _gap;
				count ++;
			}
			var sprite:Sprite = new Sprite();
			sprite.addChild(dis);
			var rect:Rectangle = dis.getRect(dis.parent);
			dis.x -=  (rect.x + rect.width / 2);
			dis.y -=  (rect.y + rect.height / 2);
			return sprite;
		}
		
		
		static private function getScore(frame:int, grounding:uint = 0x0D374F, scoreColor:uint = 0xFFCC00):MovieClip
		{
			var resource:Class = Class(getDefinitionByName("com.boyaa.shells.view.ViewScore" + frame));
			var score:MovieClip = returnMC(frame);
			
			//var color:ColorTransform = new ColorTransform();
			//color.color = grounding;
			//score.background.transform.colorTransform = color;
			//color.color =  scoreColor;
			//score.scoreMC.transform.colorTransform = color;
			return score;
		}
		
		
		/**
		 *  返回對應數字
		 * @param	type
		 * @return
		 */
		static private function returnMC(type:int):MovieClip
		{
			var mc:MovieClip;
			for (var i:int = 0; i < _allMC.length; i++) 
			{
				var arr:Array = _allMC[i];
				if (arr[0] == type)
				{
					for (var j:int = 1; j < arr.length; j++) 
					{
						mc = arr[j];
						if (mc.parent == null)
						{
							return mc;
						}
					}
					var resource:Class = Class(getDefinitionByName("com.boyaa.shells.view.ViewScore" + type));
					mc =  new resource();
					arr.push(mc);
					return mc;
				}
			}
			var resource1:Class = Class(getDefinitionByName("com.boyaa.shells.view.ViewScore" + type));
			mc =  new resource1();
			_allMC.push([type, mc]);
			return mc;
		}
		
		
		
	}
}